package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IMedcineDetailDAO;
import com.trkj.dao.jpa.ISSAllBuyDrugsDAO;
import com.trkj.dao.jpa.ISSBuyDrugDetails;
import com.trkj.dao.jpa.ISSLackDrugDAO;
import com.trkj.dao.jpa.IStaffDAO_zjs;
import com.trkj.dao.mybatis.SSQueYaoDAO;
import com.trkj.pojos.DrugstorageAllbuymedcine;
import com.trkj.pojos.DrugstorageBuymedcineDetail;
import com.trkj.pojos.DrugstorageLackDrug;
import com.trkj.pojos.MedcineDetail;
import com.trkj.pojos.Staff;
import com.trkj.vo.NameToNumber_zjs;

/**
 * 药库缺药service
 * @author Administrator
 *
 */
@Service
@Transactional
public class SSQueYaoService {
	//药库缺药MybatisDAO
	@Autowired
	private SSQueYaoDAO ssQueYaoDAO;
	@Autowired
	private ISSLackDrugDAO issLackDrugDAO;
	@Autowired
	private IMedcineDetailDAO medcineDetailDAO;
	@Autowired
	private IStaffDAO_zjs staffDAO_zjs;
	@Autowired
	private ISSAllBuyDrugsDAO allBuyDrugsDAO;
	@Autowired
	private ISSBuyDrugDetails issBuyDrugDetails;
	 /**
	 * 新增一条缺药记录
	 * @param medcineId
	 * @param medcineNumber
	 * @param staffId
	 * @param note
	 */
	public void addNewSSLackDrug(Long medcineId,BigDecimal medcineNumber,String staffId,String note) {
		MedcineDetail medcineDetail = medcineDetailDAO.findOne(medcineId);
		Staff staff = staffDAO_zjs.findOneStaffById(staffId);
		DrugstorageLackDrug lackDrug = new DrugstorageLackDrug(medcineNumber.multiply(medcineDetail.getMedcinePrice()), note, medcineNumber);
		lackDrug.setStaff(staff);
		lackDrug.setMedcineDetail(medcineDetail);
		issLackDrugDAO.save(lackDrug);
	}
	/**
	 * 获取所有的药品详情
	 * @return
	 */
	public List<MedcineDetail> getAllMedcineDetail(){
		return ssQueYaoDAO.getAllMedcineDetail();
	}
	/**
	 * 分页获取所有的药库缺药详情
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map getAllSSQueYaoMap(int page,int rows) {
		Page<DrugstorageLackDrug> p =PageHelper.startPage(page, rows, true);
		List<DrugstorageLackDrug> list=ssQueYaoDAO.getAllSSQueYao();
		Map map =new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", this.mm(list));
		return map;
	}
	public List<Map> mm(List<DrugstorageLackDrug> list){
		List<Map> lists = new ArrayList<>();
		if(!list.isEmpty()) {
			String s= JSON.toJSONString(list);
			JSONArray arr = JSON.parseArray(s);
			for(int i = 0;i<arr.size();i++) {
				Map map1 = new HashMap<>();
				JSONObject obj = arr.getJSONObject(i);
				Long drugstorageLackdrugsId = obj.getLong("drugstorageLackdrugsId");
				BigDecimal medcineCost = obj.getBigDecimal("medcineCost");
				String medcineNote = obj.getString("medcineNote");
				BigDecimal medcineNumber = obj.getBigDecimal("medcineNumber");
				JSONObject medcineDetail = obj.getJSONObject("medcineDetail");
				JSONObject staff = obj.getJSONObject("staff");
				JSONObject medcineType = medcineDetail.getJSONObject("medcineType");
				Long medcineId = medcineDetail.getLong("medcineId");
				String medcineName = medcineDetail.getString("medcineName");
				Long medcinetypeId = medcineType.getLong("medcinetypeId");
				String medcineTypeName = medcineType.getString("medcineTypeName");
				Long staffId= staff.getLong("staffId");
				String staffname = staff.getString("staffname");
				map1.put("drugstorageLackdrugsId", drugstorageLackdrugsId);
				map1.put("medcineCost", medcineCost);
				map1.put("medcineNote", medcineNote);
				map1.put("medcineNumber", medcineNumber);
				map1.put("medcineId", medcineId);
				map1.put("medcineName", medcineName);
				map1.put("medcinetypeId", medcinetypeId);
				map1.put("medcineTypeName", medcineTypeName);
				map1.put("staffId", staffId);
				map1.put("staffname", staffname);
				lists.add(map1);
			}
		}
		return lists;
	}
	/**
	 * 按条件分页查询缺药详情
	 * @param page
	 * @param rows
	 * @param map
	 * @return
	 */
	public Map querySomeSSLackDrug(int page,int rows,Map map) {
		Page<DrugstorageLackDrug> p = PageHelper.startPage(page, rows, true);
		List<DrugstorageLackDrug> lackDrugs = ssQueYaoDAO.getSSQueYaoBySome(map);
		Map map2 = new HashMap<>();
		map2.put("total", p.getTotal());
		map2.put("rows", this.mm(lackDrugs));
		return map2;
	}
	/**
	 * 按药品名称查询药库缺药详情
	 * @param page
	 * @param rows
	 * @param medcineName
	 * @return
	 */
	public Map QuerySSLackDrugByName(int page,int rows,Map map) {
		Page<DrugstorageLackDrug> p = PageHelper.startPage(page, rows, true);
		List<DrugstorageLackDrug> lackDrugs = ssQueYaoDAO.getSSQueYaoByDrugName(map);
		Map map2 = new HashMap<>();
		map2.put("total", p.getTotal());
		map2.put("rows", this.mm(lackDrugs));
		return map2;
	}
	/**
	 * 分页查询药库计划购药详情
	 * @param page
	 * @param rows
	 * @return
	 */
	public Map QueryAllSSBuyDrugDetailMap(int page,int rows) {
		Page<DrugstorageBuymedcineDetail> p = PageHelper.startPage(page, rows, true);
		List<DrugstorageBuymedcineDetail> lists = ssQueYaoDAO.getAllSSBuyDrugsDetails();
		Map map = new HashMap<>();
		map.put("total", p.getTotal());
		map.put("rows", this.fastJieXi(lists));
		return map;
	}
	/**
	 * 新增药库下一批计划采购详情
	 * @param medcineName
	 * @param medcineNumber
	 */
	public void addNewSSBuyDrugDetail(String medcineName,String medcineNumber) {
		//先判断计划采购总表是否存在
		List<DrugstorageAllbuymedcine> lists= ssQueYaoDAO.getDrugstorageAllbuymedcine();
		if(!lists.isEmpty()) {
			//如果DrugstorageAllbuymedcine不为空，则取最近的采购计划总编号
			DrugstorageAllbuymedcine da = lists.get(0);
			String[] drugName = medcineName.split("-");
			String[] drugNumber = medcineNumber.split("-");
			//判断药库计划采购药瓶详情里面是否有这种药
			//如果有这种药则修改其数量，否则新增
			List<NameToNumber_zjs> number_zjs_lists = new ArrayList<>();
			for(int i = 0;i<drugName.length;i++) {
				NameToNumber_zjs number_zjs = new NameToNumber_zjs(drugName[i], new BigDecimal(drugNumber[i]));
				number_zjs_lists.add(number_zjs);
			}
			List<DrugstorageBuymedcineDetail> DrugstorageBuymedcineDetails = ssQueYaoDAO.getAllSSBuyDrugsDetails();
			if(!DrugstorageBuymedcineDetails.isEmpty()) {
				for(int i = 0;i<number_zjs_lists.size();i++) {
					boolean test = false;
					for(int j=0;j<DrugstorageBuymedcineDetails.size();j++) {
						if(number_zjs_lists.get(i).getMedcineName().equals(DrugstorageBuymedcineDetails.get(j).getMedcineDetail().getMedcineName())) {
							test = true;
							break;
						}
					}
					if(test) {
						Map map = new HashMap<>();
						map.put("medcineName", number_zjs_lists.get(i).getMedcineName());
						Map map1 = new HashMap<>();
						map1.put("medcineId", ssQueYaoDAO.selectDrugIdByDrugName(map));
						map1.put("medcineNumber", number_zjs_lists.get(i).getMedcineNumber());
						ssQueYaoDAO.updateDrugstorageBuyMedcineNumber(map1);
					}else {
						Map map1 = new HashMap<>();
						map1.put("medcineName", number_zjs_lists.get(i).getMedcineName());
						DrugstorageBuymedcineDetail detail = new DrugstorageBuymedcineDetail(number_zjs_lists.get(i).getMedcineNumber(), da, medcineDetailDAO.findOne(ssQueYaoDAO.selectDrugIdByDrugName(map1)));
						issBuyDrugDetails.save(detail);
					}
				}
			}else {
				for(int i=0;i<number_zjs_lists.size();i++) {
					Map map = new HashMap<>();
					map.put("medcineName", number_zjs_lists.get(i).getMedcineName());
					DrugstorageBuymedcineDetail detail = new DrugstorageBuymedcineDetail(number_zjs_lists.get(i).getMedcineNumber(), da, medcineDetailDAO.findOne(ssQueYaoDAO.selectDrugIdByDrugName(map)));
					issBuyDrugDetails.save(detail);
				}
			}
		}
	}
	/**
	 * 删除多条药库缺药详情
	 * @param SSLackDrugId
	 */
	public void deleteSSLackDrug(String SSLackDrugId) {
		if(!SSLackDrugId.isEmpty()) {
			String[] s = SSLackDrugId.split("-");
			Long[] id = new Long[s.length];
			for(int i=0;i<id.length;i++) {
				id[i]=Long.parseLong(s[i]);
			}
			for(int j=0;j<id.length;j++) {
				issLackDrugDAO.delete(id[j]);
			}
		}
	}
	public List<Map> fastJieXi(List list){
		List lists = new ArrayList<>();
		if(!list.isEmpty()) {
			String s = JSON.toJSONString(list);
			JSONArray arr = JSON.parseArray(s);
			for(int i = 0;i<list.size();i++) {
				JSONObject obj = arr.getJSONObject(i);
				Map map = new HashMap<>();
				BigDecimal buymedcineMedcinenumber = obj.getBigDecimal("buymedcineMedcinenumber");
				Long drugStorageId = obj.getLong("drugStorageId");
				JSONObject drugstorageAllbuymedcine = obj.getJSONObject("drugstorageAllbuymedcine");
				String allbuymedcineName = drugstorageAllbuymedcine.getString("allbuymedcineName");
				Long drugstorageAllbuymedcineId = drugstorageAllbuymedcine.getLong("drugstorageAllbuymedcineId");
				JSONObject medcineDetail = obj.getJSONObject("medcineDetail");
				String medcineName = medcineDetail.getString("medcineName");
				Long medcineId = medcineDetail.getLong("medcineId");
				map.put("medcineNumber", buymedcineMedcinenumber);
				map.put("drugStorageId", drugStorageId);
				map.put("allbuymedcineName", allbuymedcineName);
				map.put("drugstorageAllbuymedcineId", drugstorageAllbuymedcineId);
				map.put("medcineName", medcineName);
				map.put("medcineId", medcineId);
				lists.add(map);
			}
		}
		return lists;
	}
}
